<?php
namespace Chat\Controller;
use Think\Controller;

class BaseController extends Controller{

    public $user_id = 0;
    public $openid = '';
    public $user_type = 'customer';

    public function _initialize(){

        header("Content-type: text/html; charset=utf-8");
        if(I('openid')){
            $openid = I('openid');
            session('openid', $openid);
        }else{
            $openid = session('openid');
            if(empty($openid)){
                // $openid = 'oYCoAxOQ1ViEmFsAw7Ly3OU85B-o';//session('openid');
                if(empty($openid)){
                   
                }
            }
        }
        if(in_array(ACTION_NAME, ['lgback'])){
            
        }else{
            if(empty($openid)){
                $this->wlg();
            }
            //判断是否是客服,如果是就直接跳转过去
            $kefu = M('kf')->where('openid="'.$openid.'"')->find();        

            $customer=M('customer')->where('openid="'.$openid.'"')->find();
            if (empty($customer)) {
                $this->redirect('../index.php/Register/index');
            }

            if(empty($kefu) && empty($customer)){
                $this->redirect('../index.php/Register/index');
            }
            if($kefu){
                $this->user_type = 'kefu';
            }
            // if($customer['login'] == 0){
            //     $this->redirect('../index.php/Enter/index');
            // }
            $this->openid = $openid;
            $this->user_id = $customer['id'];

            $userInfo = M('user')->where(['openid'=>$openid])->find();
            if($userInfo){
                $user = [
                    'nickname'  => $userInfo['nickname'],
                    'avatar'    => $userInfo['headimgurl'],
                ];
            }else{
                $user = [
                    'nickname'  => '',
                    'avatar'    => '',
                ];
            }
            if($this->user_type == 'kefu'){
                $user = [
                    'nickname'  => $kefu['kf_nickname'],
                    'avatar'    => $kefu['kf_headimgurl'],
                ];
            }
            // $user_id = I('user_id', 0);
            // if($user_id){
            //     cookie('user_id', $user_id);
            // }
            // $user_id = cookie('user_id') ? cookie('user_id') : 0;
            // $this->user_id = $user_id;
            $this->assign('user_id', $this->user_id);
            $this->assign('user', $user);
        }

    }

    public function wlg(){
        $current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $callback = U('lgback', [], true, true);
        $callback.="?callback=".urlencode($current_url);
        $weObj = new \Org\Util\Wechat(C('options'));
        $url = $weObj->getOauthRedirect($callback);
        header('location:'.$url);
        exit('');
    }

    public function lgback(){ 
        $callback = I('callback');
        $weObj = new \Org\Util\Wechat(C('options'));
        $tk = $weObj->getOauthAccessToken();
        session('openid', $tk['openid']);
        if($callback){
            header('location:'.urldecode($callback));
        }
        exit('');
    }

    public function customerInfo($customer_id)
    {
        $customer=M('customer')->where(['id'=>$customer_id])->find();
        if($customer){

            $weObj = new \Org\Util\Wechat(C('options'));
            $userinfo = $weObj->getUserInfo($customer['openid']);
            if($userinfo){
                $customer['nickname'] = $userinfo['nickname'];
                $customer['avatar'] = $userinfo['headimgurl'];
            }
            if(!isset($customer['nickname'])){
                $customer['nickname'] = '';
            }
            if(!isset($customer['avatar'])){
                $customer['avatar'] = '';
            }
            if($customer['note_name']){
                $customer['name'] = $customer['note_name'];
            }

            $kefu = M('kf')->where(['openid'=>$customer['openid']])->count();
            if($kefu){
                $customer['customer_type'] = 'kefu';
            }else{
                $customer['nickname'] = $customer['name'];
            }
            $customer['customer_type'] = 'customer';
            $kf = M('kf')->where(['openid'=>$customer['openid']])->find();
            if($kf){
                $customer['kf_id'] = $kf['id'];
            }
        }
        return $customer;
    }

    public function customerType($customer_id)
    {
        $customer=M('customer')->where(['id'=>$customer_id])->find();
        if($customer){
            $weObj = new \Org\Util\Wechat(C('options'));
            $userinfo = $weObj->getUserInfo($customer['openid']);
            if($userinfo){
                $customer['nickname'] = $userinfo['nickname'];
                $customer['avatar'] = $userinfo['headimgurl'];
            }
            if(!isset($customer['nickname'])){
                $customer['nickname'] = '';
            }
            if(!isset($customer['avatar'])){
                $customer['avatar'] = '';
            }
            $kefu = M('kf')->where(['openid'=>$customer['openid']])->count();
            if($kefu){
                return 'kefu';
            }
            return 'customer';
        }
        return '';
    }
}